<template>
    <el-dialog
            :title="!dataForm.taskId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="起运地" prop="startLocation">
                        <el-input v-model="dataForm.startLocation" placeholder="起运地"></el-input>
                </el-form-item>
                <el-form-item label="目的地" prop="endLocation">
                        <el-input v-model="dataForm.endLocation" placeholder="目的地"></el-input>
                </el-form-item>
                <el-form-item label="运输距离（公里）" prop="distance">
                        <el-input v-model="dataForm.distance" placeholder="运输距离（公里）"></el-input>
                </el-form-item>
                <el-form-item label="运输成本" prop="cost">
                        <el-input v-model="dataForm.cost" placeholder="运输成本"></el-input>
                </el-form-item>
                <el-form-item label="任务状态" prop="status">
                    <el-select v-model="dataForm.status" placeholder="请选择任务状态">
                        <el-option label="待处理" value="待处理"></el-option>
                        <el-option label="运输中" value="运输中"></el-option>
                        <el-option label="已完成" value="已完成"></el-option>
                        <el-option label="已取消" value="已取消"></el-option>
                    </el-select>
                </el-form-item>                
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            taskId: null,
            planId: null,
            orderId: null,
            vehicleId: null,
            driverId: null,
            carrierId: null,
            startLocation: null,
            endLocation: null,
            distance: null,
            cost: null,
            status: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            taskId: [
                        { required: true, message: '运输任务唯一标识不能为空', trigger: 'blur' }
            ],
            planId: [
                        { required: true, message: '所属运输计划ID不能为空', trigger: 'blur' }
            ],
            orderId: [
                        { required: true, message: '关联订单ID不能为空', trigger: 'blur' }
            ],
            vehicleId: [
                        { required: true, message: '分配的车辆ID不能为空', trigger: 'blur' }
            ],
            driverId: [
                        { required: true, message: '分配的司机ID不能为空', trigger: 'blur' }
            ],
            carrierId: [
                        { required: true, message: '承运商ID不能为空', trigger: 'blur' }
            ],
            startLocation: [
                        { required: true, message: '起运地不能为空', trigger: 'blur' }
            ],
            endLocation: [
                        { required: true, message: '目的地不能为空', trigger: 'blur' }
            ],
            distance: [
                        { required: true, message: '运输距离（单位：公里）不能为空', trigger: 'blur' }
            ],
            cost: [
                        { required: true, message: '运输成本不能为空', trigger: 'blur' }
            ],
            status: [
                        { required: true, message: '任务状态（待处理、运输中、已完成、已取消）不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.taskId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.taskId = id || 0
            if (this.dataForm.taskId) {
              this.$http({
                url: this.$http.adornUrl(`/app/transport_tasks/info/${this.dataForm.taskId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptransporttasks
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/transport_tasks/${!this.dataForm.taskId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>